package com.hlm.demo.controller.cost;


import com.hlm.demo.common.ResultWrapper;
import com.hlm.demo.entity.CostInfo;
import com.hlm.demo.service.cost.ICostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/fee")
public class CostController {

    @Autowired
    private ICostService costService;


    /**
     * @param order 排序方式
     * @param page 页码
     * @return
     */
    @RequestMapping("/showCost")
    @ResponseBody
    public Object showCost(@RequestParam(defaultValue = "cost_id") String order, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "3") Integer count) {
        // 分页数据
        List<CostInfo> costInfoList = costService.costCheck(order, page, count);
        System.out.println(costInfoList);
        // 数据总条目数
        Integer total = costService.costTotalCheck();
        return ResultWrapper.success(total, costInfoList);

    }

    /**
     * 更改资费
     * @param costInfo
     * @return
     */
    @RequestMapping(value = "/modifyCost", method = RequestMethod.POST)
    public String modifyCost(@Validated CostInfo costInfo, BindingResult result) {

        if (result.hasErrors()){
            System.out.println("更新资费出现错误了*******----------**-*-*-");
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(System.out::println);
        }else{
        costService.costModify(costInfo);
        }

        return "redirect:/fee/fee_list.html";
    }

    /**
     * 启用暂停开关
     * @return
     */
    @RequestMapping(value = "/costSwitch", method = RequestMethod.POST)
    public String costSwitch(String costId, Integer costStatus) {

        costService.costSwitch(costId, costStatus);

        return "redirect:/fee/fee_list.html";
    }

    /**
     * 添加资费
     * @param costInfo
     * @return
     */
    @RequestMapping(value = "/costAdd", method = RequestMethod.POST)
    public String costAdd(@Validated CostInfo costInfo,BindingResult result) {
        if (result.hasErrors()) {
            List<FieldError> errors = result.getFieldErrors();
            errors.forEach(System.out::println);
        } else {
        costService.costAdd(costInfo);
        }


        return "redirect:/fee/fee_list.html";
    }


}
